﻿Public Class SelectFile
    Public Sub collaspe()
        Tree.ExpandAll()
    End Sub

    Private Sub ElementToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ElementToolStripMenuItem.Click
        Dim op = InputBox("请输入类型(int),最小值,最大值,(可选)变量 注意,英文逗号").Split(",")
        Dim root = Tree.SelectedNode.Nodes.Add("Element()")
        root.Nodes.Add(op(0))
        root.Nodes.Add(op(1))
        root.Nodes.Add(op(2))
        If op.Length = 4 Then
            root.Nodes.Add(op(3))
            VarPool.Items.Add(Tree.SelectedNode.Index & " : " & root.Index & " : " & op(3))
        End If
        collaspe()
    End Sub

    Private Sub FileSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileSelect.Click
        Dim dialog = New OpenFileDialog
        dialog.Filter = "std exe (*.exe)|*.exe"
        dialog.ShowDialog()
        StdFile.Text = dialog.FileName
    End Sub

    Private Sub 单行ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 单行ToolStripMenuItem.Click
        Dim root = Tree.Nodes.Add("Line()")
    End Sub

    Private Sub 删除ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem.Click
        Tree.SelectedNode.Remove()
    End Sub

    Private Sub Build_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Build.Click
        'Debug.Print(Tree.Nodes.Count)
        'Exit Sub
        Dim result1, result2, result3 As String
        result1 = "{" + vbCrLf + "    ""name"":""" + ProjectName.Text + """," + vbCrLf + "    ""test_count"":""" + TestNumber.Value.ToString + ""","
        result2 = "    ""std_exe"":""" + StdFile.Text + """," + vbCrLf + "    ""output_file_ext"":""" + ExtName.Text + """," + vbCrLf + "    ""data_generate"":["
        result3 = ""
        Dim globaldot As Boolean
        globaldot = False
        For line = 0 To (Tree.Nodes.Count - 1)
            Dim fst = ""
            If globaldot = True Then
                fst = fst + "," + vbCrLf
            Else
                globaldot = True
            End If
            fst = fst + "        {""type"":"""
            If Tree.Nodes.Item(line).Text = "Line()" Then
                fst = fst + "line"
            ElseIf Tree.Nodes.Item(line).Text = "Array()" Then
                fst = fst + "array"
            End If
            fst = fst + """,""data"":["
            'Debug.Print(fst)
            'Debug.Print(line.ToString + "-" + Tree.Nodes.Item(line).Text)
            Dim dot As Boolean
            dot = False
            For element = 0 To Tree.Nodes.Item(line).Nodes.Count - 1
                If dot = True Then
                    fst = fst + ","
                Else
                    dot = True
                End If
                Dim curr_node = Tree.Nodes.Item(line).Nodes.Item(element).Nodes
                fst = fst + "{""type"":""" + curr_node.Item(0).Text + """,""min"":""" + curr_node.Item(1).Text + """,""max"":""" + curr_node.Item(2).Text + """"
                If curr_node.Count = 4 Then
                    fst = fst + ",""var"":""" + curr_node.Item(3).Text + """}"
                Else
                    fst = fst + "}"
                End If
                'MsgBox(line.ToString + "-" + element.ToString)
            Next
            fst = fst + "]}"
            'Debug.Print(fst)
            result3 = result3 + fst
        Next
        'MsgBox(Tree.Nodes.Count)
        Result.Text = result1 + vbCrLf + result2 + vbCrLf + result3 + vbCrLf + "    ]" + vbCrLf + "}"
        '"std_exe" : "std.exe",
        '"output_file_ext" :"test",
        '"data_generate" : [
    End Sub

    Private Sub Save_out_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save_out.Click
        Dim dg = New SaveFileDialog
        dg.Filter = "json file (*.json)|*.json"
        dg.ShowDialog()
        'MsgBox(dg.FileName)
        Dim f As New IO.StreamWriter(dg.FileName, True, System.Text.Encoding.UTF8)
        f.Write(Result.Text)
        f.Close()
    End Sub
End Class
